package com.system.demo.controller.competition;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.system.demo.entity.DTO.ApplicationIdDTO;
import com.system.demo.entity.DTO.ApplicationSearchDTO;
import com.system.demo.entity.DTO.CognizantIdDTO;
import com.system.demo.service.CompetitionService;
import com.system.demo.tool.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("competition/admin")
public class CompetitionAdminController {

    @Autowired
    private CompetitionService competitionService;

    /**
     * 查询学科竞赛申报情况：
     *
     * @param applicationSearchDTO 筛选项
     * @param current 当前页
     * @param size 页容量
     * @return 返回信息
     */
    @PostMapping("/get")
    @SaCheckPermission("CompApprove") //需要有竞赛审批的权限
    //该接口只能由ROLE_DEPUTY_ADMIN与ROLE_ADMIN访问
//    @SaCheckRole(value = {"ROLE_DEPUTY_ADMIN", "ROLE_ADMIN"}, mode = SaMode.OR)
    public ResponseResult<?> searchAllCompetitionApplications(@RequestBody ApplicationSearchDTO applicationSearchDTO, int current, int size){
        return competitionService.searchAllCompetitionApplications(applicationSearchDTO, current, size);
    }
    /**
     * 学科竞赛申报审核：
     *
     * @param applicationId 前端提交的竞赛申请表单
     * @return 返回信息
     */
    @PutMapping
    @SaCheckPermission("CompApprove") //需要有竞赛审批的权限
    public ResponseResult<?> reviewApplication(@RequestBody ApplicationIdDTO applicationId, @RequestParam Integer isPass){
        return competitionService.reviewApplication(applicationId, isPass);
    }

    /**
     * 学科竞赛等级认定：
     *
     * @param applicationId 要认定的申请表号
     * @param finalRankName 认定级别（国家 省部 校级）
     * @param finalRankLevel 特等 一等 二等 三等 优胜
     * @return 返回信息
     */
    @PostMapping
    @SaCheckPermission("CompApprove") //需要有竞赛审批的权限
    public ResponseResult<?> cognizance(@RequestParam Integer applicationId, @RequestParam Integer isPass, @RequestParam Integer finalRankName, @RequestParam Integer finalRankLevel){
        return competitionService.cognizance(applicationId, isPass, finalRankName, finalRankLevel);
    }


    /**
     * 导出竞赛申报信息excel：
     *
     * @param applicationSearchDTO 筛选条件 根据这些筛选条件筛出的List，然后把这些List导出！
     */
    @SaCheckPermission("CompApprove") //需要有竞赛审批的权限
    @PostMapping("/export")
    public void exportApplicationExcel(@RequestBody ApplicationSearchDTO applicationSearchDTO, HttpServletResponse response){
        competitionService.exportApplicationExcel(applicationSearchDTO, response);
    }

    //添加一个管理员修改认定结果
    @PostMapping("/cognizanceCheck")
    @SaCheckRole("ROLE_ADMIN")
    public ResponseResult<?> cognizanceCheck(@RequestParam Integer applicationId, @RequestParam Integer isPass, @RequestParam Integer finalRankName, @RequestParam Integer finalRankLevel){
        return competitionService.cognizanceCheck(applicationId, isPass, finalRankName, finalRankLevel);
    }

    //根据applicationId查找详细信息，特别是认定的时候，我需要查看提交的证明 等等
    @GetMapping("/cognizanceInfo")
    @SaCheckPermission("CompApprove") //需要有竞赛审批的权限
    public ResponseResult<?> getCognizanceInfo(@RequestParam Integer applicationId){
        return competitionService.getCognizanceInfo(applicationId);
    }

    //管理员可以一键下载 认定通过 的人的认定材料 前端会传一个数组 里面是applicationId
    @PostMapping("/downloadCognizantFile")
    @SaCheckPermission("CompApprove") //需要有竞赛审批的权限
    public void downloadCognizantFile(@RequestBody CognizantIdDTO cognizantIdDTO, HttpServletResponse response){
         competitionService.downloadCognizantFile(cognizantIdDTO, response);
    }

    //管理员查看某id的认定结果
    @GetMapping("/getCognizanceResult")
    @SaCheckPermission("CompApprove") //需要有竞赛审批的权限
    public ResponseResult<?> getCognizanceResult(@RequestParam Integer applicationId){
        return competitionService.getCognizanceResultByAdmin(applicationId);
    }

}
